﻿namespace UFIDA.U9.Cust.U9Demo.DeliveryBP
{
    using System;
    using System.Collections.Generic;
    using System.Text;
    using UFSoft.UBF.AopFrame;
    using UFSoft.UBF.Business;
    using UFSoft.UBF.Util.Context;

    /// <summary>
    /// StatusUpdate partial 
    /// </summary>	
    public partial class StatusUpdate 
	{	
		internal BaseStrategy Select()
		{
			return new StatusUpdateImpementStrategy();	
		}		
	}
	
	#region  implement strategy	
	/// <summary>
	/// Impement Implement
	/// 
	/// </summary>	
	internal partial class StatusUpdateImpementStrategy : BaseStrategy
	{
		public StatusUpdateImpementStrategy() { }

		public override object Do(object obj)
		{						
			StatusUpdate bpObj = (StatusUpdate)obj;

            //get business operation context is as follows
            //IContext context = ContextManager.Context	

            //auto generating code end,underside is user custom code
            //and if you Implement replace this Exception Code...
            if (bpObj == null)
                return null;
            bool isSucced = false;
            if (bpObj.ID <= 0)
            {
                throw new Exception("传入ID不能为空");
            }
            //if (bpObj.CustSaleOrder != null)
            //{
            //    CustSaleBE.CustSaleOrder custSaleOrder = bpObj.CustSaleOrder.GetEntity();
            //}
            UFIDA.U9.Cust.U9Demo.DeliverBE.Delivery custSaleOrder = UFIDA.U9.Cust.U9Demo.DeliverBE.Delivery.Finder.FindByID(bpObj.ID);
            //CustSaleBE.CustSaleOrder custSaleOrder = CustSaleBE.CustSaleOrder.Finder.Find("ID=" + bpObj.ID + "");
            //CustSaleBE.CustSaleOrder custSaleOrder1 = CustSaleBE.CustSaleOrder.Finder.Find("DocumentType.Code='单据类型编码'");
            //CustSaleBE.CustSaleOrder custSaleOrder2 = CustSaleBE.CustSaleOrder.Finder.Find("CustSaleOrderLine.Customer.Code='客户编码'");
            if (custSaleOrder != null)
            {
                using (ISession session = Session.Open())
                {
                    if (custSaleOrder.SysVersion == bpObj.Version)
                    {
                        if (custSaleOrder.DocType.ConfirmType == UFIDA.U9.Base.Doc.ConfirmTypeEnum.ApproveFlow)
                        {
                           
                            //启用工作流的方法
                            switch (custSaleOrder.Status.Value)
                            {
                                case 0://提交
                                    custSaleOrder.Status = DeliverBE.StatusEnum.Approving;
                                    //custSaleOrder.StateMachineInstance.OpenState_SubmitEventDriven(new CustSaleBE.SubmitEvent());
                                    break;
                                case 1://审核按钮

                                    break;
                                case 2://弃审按钮
                                    custSaleOrder.Status = DeliverBE.StatusEnum.Opend;
                                    //custSaleOrder.ApprovedBy = "";
                                    //custSaleOrder.ApprovedOn = DateTime.MinValue;
                                    //custSaleOrder.StateMachineInstance.ApproveState_UnApproveEventDriven(new CustSaleBE.UnApproveEvent());
                                    break;
                            }
                        }
                        else
                        {
                            switch (custSaleOrder.Status.Value)
                            {
                                case 0://提交
                                    custSaleOrder.Status = DeliverBE.StatusEnum.Approving;
                                    break;
                                case 1://审核按钮
                                    custSaleOrder.Status = DeliverBE.StatusEnum.Approved;
                                    break;
                                case 2://弃审按钮
                                    custSaleOrder.Status = DeliverBE.StatusEnum.Opend;
                                    break;
                            }
                        }
                    }
                    else
                    {
                        throw new Exception("当前数据已被其他人修改，请刷新后再操作！");
                    }
                    //custSaleOrder.Remove();//删除
                    session.Commit();
                }
            }
            else
            {
                throw new Exception("单据不存在");
            }
            isSucced = true;
            return isSucced;
        }		
	}

	#endregion
	
	
}